/**
 * Created by lintao_alex on 2017/9/19.
 */
namespace mengdao {
    export class BaseNetCommandMapper {
        private _pcMap: Map<string, [any, any]>;
        private _pushMaps: string[];

        public constructor() {
            this.initMap();
            this.mapCommands();
        }

        protected mapCommands(): void {

        }

        private initMap() {
            this._pcMap = new Map<string, [any, any]>();
            this._pushMaps = [];
        }

        public mapRequire(command: string, param: any = null, result: any = null) {
            this._pcMap.set(command, [param, result]);
        }

        public mapPush(command: string, result: any = null) {
            this.mapRequire(command, null, result);
            this._pushMaps.push(command);
        }

        public isPush(command: string): boolean {
            return this._pushMaps.indexOf(command) >= 0;
        }

        public getResponseClass(command: string): any {
            let mapV = this._pcMap.get(command);
            if (mapV) {
                return mapV[1];
            }
            return null;
        }

        public stringifyProto(protoObj: any): string {
            let jsonObj = protoObj;
            if (protoObj.toJSON) {
                jsonObj = protoObj.toJSON();
            }
            return JSON.stringify(jsonObj);
        }
    }
}